package cas.ihep.hmss;

import cas.ihep.hmss.proto.HmssProtos;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;

/**
 * Created by chocolate on 2017/12/3.
 */
public class Client {
    private static HmssProtos.JobSubmitRequest parser(String[] args){

        Options options = new Options();
        options.addOption("h","help",false,"Usage information.");
        options.addOption("u","user",true,"User of Job");
        options.addOption("p","priority",true,"priority of job");
        options.addOption("d","destPath",true,"destPath of Job");
        //options.addOption("s","srcPath",true,"srcPath of Job");
        options.addOption("g","group",true,"group of JOb");

        BasicParser parser = new BasicParser();
        CommandLine c1;
        try {
            c1 = parser.parse(options,args);
            if (c1.getOptions().length>0){
                if(c1.hasOption('h')){
                    HelpFormatter hf = new HelpFormatter();
                    hf.printHelp("Options",options);
                    System.exit(0);
                }else {
                    HmssProtos.JobSubmitRequest.Builder builder=HmssProtos.JobSubmitRequest.newBuilder();
                    builder.setDestination(c1.getOptionValue("d"));
                    //builder.setSource(c1.getOptionValue("s"));
                    builder.addAllSource(c1.getArgList());
                    //c1.getArgList();
                    builder.setGroup(c1.getOptionValue("g"));
                    builder.setUser(c1.getOptionValue("u"));
                    builder.setPriority(Integer.parseInt(c1.getOptionValue("p")));
                    return builder.build();
                    /*job.user = c1.getOptionValue("u");
                    job.group = c1.getOptionValue("g");
                    job.priority = Integer.parseInt(c1.getOptionValue("p"));
                    job.destPath = c1.getOptionValue("d");
                    job.srcPath = c1.getOptionValue("s");*/

                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public void connect(int port,String host,final HmssProtos.JobSubmitRequest job) throws Exception{
        EventLoopGroup group = new NioEventLoopGroup();

        try {
            Bootstrap b = new Bootstrap();
            b.group(group);
            b.channel(NioSocketChannel.class);
            b.option(ChannelOption.TCP_NODELAY,true);
            b.handler(new ChannelInitializer<SocketChannel >() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline pipeline=ch.pipeline();
                    pipeline.addLast("frameEncoder",new ProtobufVarint32LengthFieldPrepender());
                    pipeline.addLast("encoder",new ProtobufEncoder());
                    pipeline.addLast("frameDecoder",new ProtobufVarint32FrameDecoder());
                    pipeline.addLast("decoder",new ProtobufDecoder(HmssProtos.JobSubmitResponse.getDefaultInstance()));
                    pipeline.addLast(new ClientHandler(job));
                }
            });
            ChannelFuture f = b.connect(host,port).sync();
            f.channel().closeFuture().sync();
        }finally {
            group.shutdownGracefully();
        }
    }

    public static  void main(String[] args) throws Exception{
//        int port = 8080;
//        if (args!=null && args.length>0){
//            try {
//                port = Integer.valueOf(args[0]);
//            }catch (NumberFormatException e){
//
//            }
//
//        }
        new Client().connect(8080,"helion01.ihep.ac.cn",parser(args));
        //new Client().connect(8080,"helion01.ihep.ac.cn",parser(args));
    }

}
